/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain an copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.orhanobut.benchmark.slice;

import com.orhanobut.benchmark.ResourceTable;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.hawk.LogInterceptor;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 主页面
 *
 * @author fjh
 * @since 2021-05-26
 */
public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel log = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    private String ms = "ms";
    private String mkey = "key";
    private String count = "Hawk.count: ";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        timeHawkInit();
        timeHawkPut();
        timeHawkGet();
        timeHawkContains();
        timeHawkCount();
        timeHawkDelete();
    }

    private void timeHawkInit() {
        long startTime = System.currentTimeMillis();
        Hawk.init(this).setLogInterceptor(new LogInterceptor() {
            @Override
            public void onLog(String message) {
                HiLog.info(log,"HAWK: " + message);
            }
        }).build();

        long endTime = System.currentTimeMillis();
        HiLog.info(log,"Hawk.init:" + (endTime - startTime) + ms);
    }

    private void timeHawkPut() {
        long startTime = System.currentTimeMillis();

        Hawk.put(mkey, "value");

        long endTime = System.currentTimeMillis();
        HiLog.info(log,"Hawk.put: " + (endTime - startTime) + ms);
    }

    private void timeHawkGet() {
        long startTime = System.currentTimeMillis();

        Hawk.get(mkey);

        long endTime = System.currentTimeMillis();
        HiLog.info(log,"Hawk.get: " + (endTime - startTime) + ms);
    }

    private void timeHawkCount() {
        long startTime = System.currentTimeMillis();

        Hawk.count();

        long endTime = System.currentTimeMillis();
        HiLog.info(log,count + (endTime - startTime) + ms);
    }

    private void timeHawkContains() {
        long startTime = System.currentTimeMillis();

        Hawk.contains(mkey);

        long endTime = System.currentTimeMillis();
        HiLog.info(log,count + (endTime - startTime) + ms);
    }

    private void timeHawkDelete() {
        long startTime = System.currentTimeMillis();

        Hawk.delete(mkey);

        long endTime = System.currentTimeMillis();
        HiLog.info(log,count + (endTime - startTime) + ms);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
